package vn.fpt.fsoft.wed.action;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import vn.fpt.fsoft.business.impl.UserBusImpl;
import vn.fpt.fsoft.entities.User;

@Controller
public class LoginController {
	@Autowired
	private UserBusImpl uBus;

	public void setuBus(UserBusImpl uBus) {
		this.uBus = uBus;
	}

	@RequestMapping(value = "/", method = RequestMethod.GET)
	public ModelAndView returnLogin() {
		return new ModelAndView("login", "command", new User());
	}
	
	@RequestMapping(value="/checklogin",method=RequestMethod.POST)
	public String loginSuccess(ModelMap model, @ModelAttribute User user, HttpServletRequest request) {
		String page = "";
		if (uBus.checkLogin(user)) {
			model.addAttribute("seUser", user);
			if (request.getSession().getAttribute("requestUrl") != null) {
				page = request.getSession().getAttribute("requestUrl").toString();
			} else
				page = "index";
		} else {
			page = "login";
		}
		return page;
	}
}
